package com.jinbai.servlet;

import com.jinbai.dao.ArticleDao;
import com.jinbai.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@WebServlet("/blogEdit.do")
public class BlogEditDoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        final String title = req.getParameter("title");
        final String type = req.getParameter("type");
        final String content = req.getParameter("editor-markdown-doc");

        User currentUser = null;
        final HttpSession session = req.getSession(false);
        if (session != null) {
            currentUser = (User) session.getAttribute("currentUser");
        }
        if (currentUser == null) {
            resp.sendRedirect("/login.html");
            return;
        }
        ArticleDao articleDao = new ArticleDao();
        int aid = articleDao.insert(currentUser.uid, title, type, new Date(), content);

        resp.sendRedirect("/blog_content.html?aid=" + aid);
    }
}
